package com.imy.touch.root.file;

import android.content.Context;
import android.util.Log;

import com.imy.common.root.RootTools;
import com.imy.common.shell.RootShell;
import com.imy.common.shell.exceptions.RootDeniedException;
import com.imy.common.shell.execution.Command;
import com.imy.common.shell.execution.Shell;
import com.imy.common.util.ALog;
import com.imy.common.util.CPUUtils;
import com.imy.common.util.CommandResult;
import com.imy.common.util.FileUtils;
import com.imy.common.util.ShellUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeoutException;

public class FilePushHelper {
    public static String ANDROID_TMP_PATH = "/data/local/tmp/roottouch";
    private static String ASSETS_PREFIX = "file://android_asset/";

    /**
     * 推送touch server到android目录下 同时启动它
     *
     * @param context
     */
    public static boolean pushTouchFile(Context context) {

        File file = new File(ANDROID_TMP_PATH);
        if (file.exists()) {
            return true;
        } else {
            String path = copyFile(context);
            if (!path.equals("")) {
                return executePushFileCommand(path);
            } else {
                return false;
            }
        }
    }

    private static boolean pushPermission() {
        CommandResult commandResult = ShellUtils.execCommand("chmod 777 -R /data/local/tmp/", true);
        ALog.ti("commandResult：" + commandResult.successMsg
                + " commandResult:" + commandResult.errorMsg);

        return true;
    }

    private static boolean executePushFileCommand(String path) {
        //复制文件到目录
        //先要提权
        if (!pushPermission()) {
            return false;
        }

        boolean b = FileUtils.copyFile(path, ANDROID_TMP_PATH);
        ALog.ti(" copyFile:"+b);
        return b ;

    }

    private static String copyFile(Context context) {
        File filesDir = context.getFilesDir();
        if (!filesDir.exists()) {
            filesDir.mkdir();
        }
        File touch = new File(filesDir.getAbsolutePath() + "/roottouch");
        if (touch.exists()) {
            return touch.getAbsolutePath();
        }
        ALog.ti("fileDir:" + filesDir.getAbsolutePath());
        try {
            InputStream is = context.getAssets()
                    .open("roottouch");
            FileOutputStream fos = new FileOutputStream(new File(filesDir.getAbsolutePath() + "/roottouch"));
            byte[] buffer = new byte[1024];
            int byteCount = 0;
            while ((byteCount = is.read(buffer)) != -1) {// 循环从输入流读取
                // buffer字节
                fos.write(buffer, 0, byteCount);// 将读取的输入流写入到输出流
            }
            fos.flush();// 刷新缓冲区
            is.close();
            fos.close();
            return touch.getAbsolutePath();
        } catch (IOException e) {
            ALog.ti("error:" + e.getMessage());
            e.printStackTrace();
            return "";
        }
    }

}
